package com.xmad.ksadutils.AdSDK.AdMold.Kwai;

import android.util.Log;

import androidx.annotation.Nullable;
import com.kwad.sdk.api.KsAdSDK;
import com.kwad.sdk.api.KsInterstitialAd;
import com.kwad.sdk.api.KsLoadManager;
import com.kwad.sdk.api.KsScene;
import com.kwad.sdk.api.KsVideoPlayConfig;
import com.xmad.ksadutils.AdSDK.AdBase.AdBase;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class KsLoadInterstitialAd2 extends AdBase {
    private volatile static KsLoadInterstitialAd2 mInstance;
    KsInterstitialAd Ad;
    static Map<Long, AdComplete> mMap= new HashMap<Long, AdComplete>();
    /**
     * 单例模式
     *
     * @return
     */
    public static KsLoadInterstitialAd2 getInstance() {
        if (mInstance == null) {
            synchronized (KsLoadInterstitialAd2.class) {
                if (mInstance == null) {
                    mInstance = new KsLoadInterstitialAd2();
                }
            }
        }
        return mInstance;
    }


    @Override
    protected void load() {
        if (getKSid()==null||getKSid().length==0){
            Log.e(TAG,"先设置梯度ID");
            return;
        }
        //新建
        NewMap();
        //获取广告对象List列表
        GetAdList();
    }

    //新建
    public void NewMap(){
        Log.e(TAG,mMap.toString());
        if (mMap.size()==0){
            for (Long Id:getKSid()){
                mMap.put(Id,null);
            }
        }
    }

    //获取广告对象List列表
    public void GetAdList(){
        for(Long key:mMap.keySet()){
            if (mMap.get(key)==null||mMap.get(key).getAd()==null){
                getAds(key);
            }
        }
    }

    private void   getAds(Long id){
        Log.e(TAG,"KsLoadInterstitialAd2加载"+ id);
        KsScene scene = new KsScene.Builder(id).build();
        KsAdSDK.getLoadManager().loadInterstitialAd(scene, new KsLoadManager.InterstitialAdListener() {
            @Override
            public void onError(int code, String msg) {
                if (listener!=null){
                    listener.onError(code+msg);
                }
            }
            @Override
            public void onRequestResult(int adNumber) {
                Log.e(TAG,"告请求填充个数");
            }
            @Override
            public void onInterstitialAdLoad(@Nullable List<KsInterstitialAd> adList) {
                if (adList != null && adList.size() > 0) {
                    Ad = adList.get(0);
                    Log.e(TAG,"插屏⼴告请求成功");
                    KsVideoPlayConfig videoPlayConfig = new
                            KsVideoPlayConfig.Builder()
                            .build();
                    // showInterstitialAd(videoPlayConfig);
                    if (Ad != null) {
                        Ad.setAdInteractionListener(new KsInterstitialAd.AdInteractionListener() {
                            @Override
                            public void onAdClicked() {
                                Log.e(TAG,"插屏⼴插屏广告点击");
                                if (listener!=null){
                                    listener.onAdClicked();
                                }
                            }

                            @Override
                            public void onAdShow() {
                                if (listener!=null){
                                    listener.onAdShow();
                                }
                            }

                            @Override
                            public void onAdClosed() {
                                Log.e(TAG,"用户点击插屏关闭按钮");
                            }

                            @Override
                            public void onPageDismiss() {
                                Log.e(TAG,"插屏广告关闭");
                                if (listener!=null){
                                    listener.onPageDismiss();
                                }
                            }

                            @Override
                            public void onVideoPlayError(int code, int extra) {
                                Log.e(TAG,"插屏广告播放出错");
                                if (listener!=null){
                                    listener.onError(code+"");
                                }
                            }

                            @Override
                            public void onVideoPlayEnd() {
                                Log.e(TAG,"插屏广告播放完成");
                                if (listener!=null){
                                    listener.onComplete();
                                }
                            }

                            @Override
                            public void onVideoPlayStart() {
                                Log.e(TAG,"插屏⼴插屏广告播放开始");
                            }

                            @Override
                            public void onSkippedAd() {
                                Log.e(TAG,"插屏⼴插屏广告播放跳过");
                            }

                        });
                        mMap.put(id,new AdComplete(true,Ad));
                        if (!isReveal()){
                            return;
                        }
                        for(Long key:mMap.keySet()) {
                            if (mMap.get(key) != null && mMap.get(key).getAd() != null) {
                                mMap.get(key).getAd().showInterstitialAd(activity, videoPlayConfig);
                                break;
                            }
                        }
                    } else {
                        Log.e(TAG,"插屏⼴暂无可用插屏广告，请等待缓存加载或者重新刷新");
                    }
                }
            }
        });
    }

    @Override
    protected void start() {

    }

    public class AdComplete{
        private boolean Complete;
        private KsInterstitialAd ad;

        public AdComplete(boolean complete, KsInterstitialAd ad) {
            Complete = complete;
            this.ad = ad;
        }

        public boolean isComplete() {
            return Complete;
        }

        public void setComplete(boolean complete) {
            Complete = complete;
        }

        public KsInterstitialAd getAd() {
            return ad;
        }

        public void setAd(KsInterstitialAd ad) {
            this.ad = ad;
        }
    }

}

